package com.pengtu.service.business;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.task.TaskImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.business.DetnIssueDao;
import com.pengtu.dao.business.DetnPetitionDao;
import com.pengtu.dao.business.DetnReportDao;
import com.pengtu.dao.business.FlowDetectionDao;
import com.pengtu.entity.app.User;
import com.pengtu.entity.business.detection.DetnIssue;
import com.pengtu.entity.business.detection.DetnPetition;
import com.pengtu.entity.business.detection.DetnReport;
import com.pengtu.entity.business.detection.FlowDetection;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.service.BaseManager;

/**
 * 交工检测相关实体的管理类
 * 
 * @author liuyang
 * 
 */
@Component
@Transactional
public class FlowDetectionManager extends BaseManager {
	private FlowDetectionDao flowDetectionDao;
	private DetnReportDao detnReportDao;
	private DetnIssueDao detnIssueDao;
	private DetnPetitionDao detnPetitionDao;

	@Autowired
	public void setFlowDetectionDao(FlowDetectionDao flowDetectionDao) {
		this.flowDetectionDao = flowDetectionDao;
	}

	@Autowired
	public void setDetnReportDao(DetnReportDao detnReportDao) {
		this.detnReportDao = detnReportDao;
	}

	@Autowired
	public void setDetnIssueDao(DetnIssueDao detnIssueDao) {
		this.detnIssueDao = detnIssueDao;
	}

	/**
	 * @param detnPetitionDao the detnPetitionDao to set
	 */
	@Autowired
	public void setDetnPetitionDao(DetnPetitionDao detnPetitionDao) {
		this.detnPetitionDao = detnPetitionDao;
	}

	/**
	 * 保存
	 * 
	 * @param flowDetection
	 */
	public void save(FlowDetection flowDetection) {
		flowDetectionDao.save(flowDetection);
	}

	public void save(DetnReport detnReport) {
		detnReportDao.save(detnReport);
	}

	public void save(DetnIssue detnIssue) {
		detnIssueDao.save(detnIssue);
	}
	
	public void save(DetnPetition detnPetition){
		detnPetitionDao.save(detnPetition);
	}
	
	public void save(FlowDetection flowDetection,DetnPetition detnPetition){
		flowDetectionDao.save(flowDetection);
		detnPetition.setFlowDetection(flowDetection);
		detnPetitionDao.save(detnPetition);
	}

	/**
	 * 查询
	 * 
	 * @param id
	 * @return
	 */
	public FlowDetection getFlowDetection(String id) {
		return flowDetectionDao.get(id);
	}

	public Page<FlowDetection> getFlowDetections(
			final Page<FlowDetection> page, final QueryFilter queryFilter) {
		return flowDetectionDao.findPage(page, queryFilter);
	}

	public List<FlowDetection> getFlowDetection(ProjectInfo projectInfo) {
		return flowDetectionDao.findBy("projectInfo", projectInfo);
	}

	public DetnIssue getDetnIssue(String id) {
		return detnIssueDao.get(id);
	}

	public List<DetnIssue> getDetnIssue(DetnReport detnReport) {
		return detnIssueDao.findBy("detnReport", detnReport);
	}

	public DetnReport getDetnReport(String id) {
		return detnReportDao.get(id);
	}
	
	public DetnPetition getDetnPetition(String id){
		return detnPetitionDao.get(id);
	}
	
	public DetnPetition findDetnPetition(FlowDetection flowDetection){
		return detnPetitionDao.findUniqueBy("flowDetection", flowDetection);
	}

	public DetnReport getDetnReport(FlowDetection flowDetection) {
		return detnReportDao.findUniqueBy("flowDetection", flowDetection);
	}

	/**
	 * 待办任务 findPersonalTasks: 适用:流程处理
	 * 
	 * @param user
	 * @param projectInfo
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public List<FlowDetection> findPersonalTasks(User user,
			ProjectInfo projectInfo) {
		List<Task> taskList = super.findPersonalTasks(user);
		List<FlowDetection> flowDetections = new ArrayList<FlowDetection>();
		for (Task task : taskList) {
			String taskId = task.getId();
			String arg = (String)((TaskImpl)task).getVariable(Constants.START_TYPE);
			if ("detection".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.DETECTION_ID);
				FlowDetection flowDetection = flowDetectionDao.get(id);
				if (flowDetection != null) {
					if (StringUtils.equals(flowDetection.getProjectInfo()
							.getId(), projectInfo.getId())) {
						flowDetection.setTaskId(taskId);
						flowDetections.add(flowDetection);
					}
				}
			}
		}
		return flowDetections;
	}
	
	public void deleteIssue(String id){
		detnIssueDao.delete(id);
	}
	
	public List<DetnIssue> getIssuesByProId(String projectId) {
		QueryFilter fileter = new QueryFilter();
		fileter.addFilter("projId", projectId);
		return detnIssueDao.find(fileter);
}
}
